//
//  EFSaleView.m
//  EnjogFood
//
//  Created by 王芳 on 17/5/1.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFSaleView.h"
#import "EFSepecialItem.h"
#import "EFGoodsDetailViewController.h"
#import "iToast.h"

const CGFloat imageWidth = 102;

@interface EFSaleView ()
@property (strong, nonatomic) UIImageView *imageV;
@property (strong, nonatomic) UILabel *titleLable;
@property (strong, nonatomic) UILabel *salePriceLabel;
@property (strong, nonatomic) UILabel *priceLabel;
@property (strong, nonatomic) UIImageView *addImageV;
@property (strong, nonatomic) EFSepecialItem *item;
@property (strong, nonatomic) UITapGestureRecognizer *tapG;
@end

@implementation EFSaleView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame Item:(EFSepecialItem *)item{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.item = item;
        [self initUI];
        [self refreshUI];
        self.userInteractionEnabled = YES;
        [self addGestureRecognizer:self.tapG];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
}

- (void)initUI{
    [self addSubview:self.imageV];
    [self addSubview:self.titleLable];
    [self addSubview:self.salePriceLabel];
    [self addSubview:self.priceLabel];
    [self addSubview:self.addImageV];
}

- (void)refreshUI{
    [self.imageV setImageWithURL:[NSURL URLWithString:self.item.img]];
    self.titleLable.text = self.item.productName;
    [self.titleLable sizeToFit];
    self.titleLable.width = self.width - 2*5;
    self.titleLable.lineBreakMode = NSLineBreakByTruncatingTail;
    self.titleLable.top = self.imageV.bottom;
    self.titleLable.left = 5;
    
    self.salePriceLabel.text = [NSString stringWithFormat:@"￥%@",self.item.salePrice];
    [self.salePriceLabel sizeToFit];
    self.salePriceLabel.width = self.width - 5 - 25;
    self.salePriceLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    self.salePriceLabel.top = self.titleLable.bottom + 8;
    self.salePriceLabel.left = 5;
    
    //带有中划线的文案
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};;
    NSString* strSalePrice = [NSString stringWithFormat:@"¥%@",self.item.price];
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:strSalePrice attributes:attribtDic];
    self.priceLabel.attributedText = attribtStr;
    [self.priceLabel sizeToFit];
    self.priceLabel.top = self.salePriceLabel.bottom + 1;
    self.priceLabel.left = 5;
    
    self.addImageV.top = self.imageV.bottom + 29;
    self.addImageV.right = self.width - 5;
    
}

- (UIImageView *)imageV{
    if (!_imageV) {
        _imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, imageWidth, imageWidth)];
        _imageV.backgroundColor = kGrayColor;
        _imageV.contentMode = UIViewContentModeScaleAspectFill;
        _imageV.clipsToBounds = YES;
    }
    return _imageV;
}

- (UILabel *)titleLable{
    if (!_titleLable) {
        _titleLable = [[UILabel alloc] init];
        _titleLable.textColor = kBlack333;
        _titleLable.font = [UIFont systemFontOfSize:13];
    }
    return _titleLable;
}

- (UILabel *)salePriceLabel{
    if (!_salePriceLabel) {
        _salePriceLabel = [[UILabel alloc] init];
        _salePriceLabel.textColor = kTextGreenColor;
        _salePriceLabel.font = [UIFont systemFontOfSize:10];
    }
    return _salePriceLabel;
}

- (UILabel *)priceLabel{
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] init];
        _priceLabel.textColor = kTextGrayColor;
        _priceLabel.font = [UIFont systemFontOfSize:10];
    }
    return _priceLabel;
}

- (UIImageView *)addImageV{
    if (!_addImageV) {
        _addImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"buy_icon_home_default"]];
        [_addImageV addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addToShopCart)]];
        _addImageV.userInteractionEnabled = YES;
    }
    return _addImageV;
}

- (UITapGestureRecognizer *)tapG{
    if (!_tapG) {
        _tapG = [[UITapGestureRecognizer alloc] init];
        [_tapG addTarget:self action:@selector(turnToPage)];
    }
    return _tapG;
}

- (void)turnToPage{
    EFGoodsDetailViewController *vc = [[EFGoodsDetailViewController alloc] initWithProductId:self.item.id wareHouseId:self.item.warehouse_id];
    [ICNavigator pushViewController:vc animated:YES];
}

- (void)addToShopCart{
    if (APP_CONTEXT.accountManager.accountInfo.token.length > 0) {
        [APP_CONTEXT.net AddCartId:self.item.id qty:[NSNumber numberWithInt:1] type:[NSNumber numberWithInt:0] OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
            if (code != 0) {
                [[iToast makeText:message] show];
            }else{
             [[iToast makeText:@"成功加入购物车"] show];
             [[NSNotificationCenter defaultCenter] postNotificationName:kAddToCartSuccess object:nil];
            }
        }];
    }else{
        //存在本地 当用户点击购物车时 sync一下
        [APP_CONTEXT.shoppingCartManager addToStoreProduct:self.item];
         [[iToast makeText:@"成功加入购物车"] show];
    }
   
}

@end
